package com.action.background;

import java.io.File;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import ztest.t;

import com.QRcodeBuilder.LoginUserVo;
import com.enetiy.Authcode;
import com.enetiy.Users;
import com.opensymphony.xwork2.ActionContext;
import com.service.LoginService;
import com.util.IPUtil;
import com.util.Md5Util;

public class AdminAction {
	
	private static final long serialVersionUID = 1L;
	private String userPhone;
	private String pwd;
	
	
	 String jsonStr="";
	private int userid;
	private String token;
	
	private String sessionId;
	private LoginService loginService;
	
	/**
	 * 
	 * 登录验证
	 * @return
	 */
	public String adminLogin(){
		return jsonStr;
		/*Map<String, Object> session = ActionContext.getContext().getSession();
		Map<String, Object> request = ActionContext.getContext().getContextMap();
		Users users;
		try {
			Users u = new Users();
			u.setUserPhone(userPhone);
			u.setPwd(pwd);
			
			String userPhone = u.getUserPhone();
			String pwd = u.getPwd();
			System.out.println(userPhone);
			System.out.println(pwd);
			users = loginService.SelectUser(u);
			
			if(users==null){
				Object name = session.get("session");
				if(name==null){
					request.put("No", "用户未登录");
					return "admin-No";
				}else{
					return "admin-Yes";
				}			
			}else{
				session.put("session", "ok");
				return "admin-Yes";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "err";
		}*/
		
	}
	
	/**
	 * 注销登录
	 * @return
	 */
	public String loginOut(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		Object object = session.get("session");
		if(object!=null){
			session.remove("session");
		}
		return "loginOut";
	}

	/**
	 * 二维码扫描检查登录
	 * @return
	 */
	public String  checkLogin(){
        /*System.out.println("uuid:" + sessionId);
        long inTime = new Date().getTime();
        Boolean bool = true;
        while (bool) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //检测登录
            LoginUserVo vo = LoginUserVo.getVo();
           Users users = vo.getLoginUserMap().get(sessionId);
           if(users != null){
                bool = false;
                jsonStr = "{\"userPhone\":\""+users.getUserPhone()+"\",\"pwd\":\""+users.getPwd()+"\"}";
                System.out.println(jsonStr);
                vo.getLoginUserMap().remove(sessionId);
            }else{
                if(new Date().getTime() - inTime > 5000){
                    bool = false;
                }
            }
        }
        System.out.println("login ok : " + jsonStr);*/
      
		return "checkLogin";
	}
	
	/**
	 * 
	 * 后台登录二维码内容生成
	 * @return
	 */
	public String QRcodeLogin(){
			//生成唯一ID
			int uuid= (int) (Math.random() * 100000);
	       System.out.println(uuid);
	       
	       String ip = IPUtil.getIp().substring(0,IPUtil.getIp().lastIndexOf("/"));
	        //二维码内容
	        String text = "{\"from\":\"Altair\",\"for\":\"bamlogin\",\"sessionId\":\""+uuid+"\",\"url\":\""+ip+"/wifi/GuanLogin/admin_sweepLogin.action\"}";
	        System.out.println(text);
	        //生成的图片访问地址
	        jsonStr = "{\"uuid\":\"" + uuid + "\",\"text\":"+text+"}";
	        
	       HashMap<String, String> uuidMap = LoginUserVo.getVo().getUuidMap();
	       	uuidMap.put(uuid+"", "ok");
	        start(uuid+"", uuidMap);
	        System.out.println(jsonStr);
		return "QRcodeLogin";
	}
	
	/**
	 * 手机客服端登录
	 */
	    public void  sweepLogin(){
	     /*HttpServletResponse response = ServletActionContext.getResponse();
	     JSONObject json = new JSONObject();
	        PrintWriter out=null;
		//TODO 验证登录
	 	try {
	 		out = response.getWriter();
			Users users= loginService.selectUserToken(token, userid);
	        if(users!=null){
	            //将登陆信息存入map
	        	System.out.println(sessionId);
	        	HashMap<String, String> uuidMap = LoginUserVo.getVo().getUuidMap();
	        	if(uuidMap.get(sessionId+"")!=null&&!uuidMap.get(sessionId+"").equals("")){
		        	HashMap<String, Users> loginUserMap = LoginUserVo.getVo().getLoginUserMap();
					Users sess = loginUserMap.get(sessionId);
		            if(sess == null){
		               loginUserMap.put(sessionId, users);
		            }
		            json.put("code", 1);
	            }else{
	            	json.put("code", 2);
	            }
	        }else{
	        	json.put("code", 0);
	        }
	        	out.print(json);
		} catch (Exception e) {
			json.put("code", -1);
			out.print(json);
			e.printStackTrace();
		}finally{
		 	out.flush();
		 	out.close();
	 	}*/
	  }
	
		/**
		 * 计时器1
		 * @param a
		 */
		 private void start(final String uuid,final HashMap<String, String> uuidMap) {
			 	final Timer timer = new Timer();
		        timer.schedule(new TimerTask() {
		            public void run() {
		                playSound();
		            }
	            private void playSound() {
	            	try {
	            		uuidMap.remove(uuid);
	    				timer.cancel();
	    			} catch (Exception e) {
	    				e.printStackTrace();
	    			}
	            }
	        }, 300000);
		   }

	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public String getJsonStr() {
		return jsonStr;
	}

	public void setJsonStr(String jsonStr) {
		this.jsonStr = jsonStr;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}


	
}
